package com.biboheart.adapter.express.support;

import com.biboheart.adapter.express.operation.Calculate;
import com.biboheart.adapter.support.utils.ObjectTypeUtils;

import java.math.BigDecimal;

final class SubtractCalculate implements Calculate<Number, Number, Number> {
    @Override
    public Number calc(Number left, Number right) {
        if (null == left) {
            return null;
        }
        if (null == right) {
            BigDecimal r = new BigDecimal(String.valueOf(left)).multiply(BigDecimal.valueOf(-1));
            if (left instanceof Double) {
                return r.doubleValue();
            } else if (left instanceof Float) {
                return r.floatValue();
            } else if (left instanceof Long) {
                return r.longValue();
            }
            return r.intValue();
        }
        if ((left instanceof Double) || (right instanceof Double)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(left, Double.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(right, Double.class))).doubleValue();
        }
        if ((left instanceof Float) || (right instanceof Float)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(left, Float.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(right, Float.class))).floatValue();
        }
        if ((left instanceof Long) || (right instanceof Long)) {
            return BigDecimal.valueOf(ObjectTypeUtils.convertType(left, Long.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(right, Long.class))).longValue();
        }
        return BigDecimal.valueOf(ObjectTypeUtils.convertType(left, Integer.class)).subtract(BigDecimal.valueOf(ObjectTypeUtils.convertType(right, Integer.class))).intValue();
    }

    @Override
    public Number calc(Number num) {
        return new BigDecimal(String.valueOf(num)).multiply(BigDecimal.valueOf(-1));
    }
}
